package org.jiamandan.com.strategy.impl.tram.cmd;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jiamandan.com.constant.BizConstant;
import org.jiamandan.com.dto.ZDBillingDto;
import org.jiamandan.com.strategy.impl.tram.TramBasicService;
import org.jiamandan.com.utils.CommonUtil;
import org.jiamandan.com.utils.ZDObjectUtil;
import org.springframework.stereotype.Service;

/**
 * @version Revision 1.0.1
 * @版权：版权所有 (c) 2021
 * @see:
 * @创建日期：2022-12-12 14:00
 * @功能说明： 7.1. CMD=501 查询计费模型
 * 7.3 CMD=503 查询计费模型（电单车）
 * 7.5 CMD=505 下发计费模型
 * 7.7 CMD=507 下发计费模型（电单车）
 * @begin
 * @修改记录:
 * @修改后版本 修改人        修改内容
 * @2022-12-01 16:50	        CWJ       	创建
 * @end
 */
@Service(value = BizConstant.CmdClass.CMD_503_SERVICE)
@AllArgsConstructor
@Slf4j
public class ZDissueBillingModeService implements TramBasicService {

    @Override
    public void doExecute(byte[] data, Channel client) {
        ZDBillingDto dto = JSON.parseObject(CommonUtil.bytesToString(data), ZDBillingDto.class);
            StringBuilder builder = new StringBuilder();
            if(dto.getCmd().equals("501")){
                builder.append(StringUtils.leftPad(dto.getGunNo(), 2,"0"));
                String payload =  ZDObjectUtil.pushMessage(builder.toString(),  dto.getPileNo(), BizConstant.Cmd.CMD_501);
                log.info("下发串CMD501:"+payload);
                ByteBuf buf = Unpooled.buffer(payload.getBytes().length);
                buf.writeBytes(CommonUtil.hexToByteArray(payload));
                ChannelFuture future = client.writeAndFlush(buf);
//                ChannelFuture future = client.writeAndFlush(payload);
                boolean issuccess = future.isDone();
                if(issuccess){
                    log.info("下发成功CMD501:"+payload);
                }else{
                    log.error("下发失败CMD501:"+payload);
                }
            }
            if(dto.getCmd().equals("503")){
                builder.append(StringUtils.leftPad(dto.getGunNo(), 2,"0"));
                String payload =  ZDObjectUtil.pushMessage(builder.toString(), dto.getPileNo(), BizConstant.Cmd.CMD_503);
                log.info("下发串CMD503:"+payload);
                ByteBuf buf = Unpooled.buffer(payload.getBytes().length);
                buf.writeBytes(CommonUtil.hexToByteArray(payload));
                ChannelFuture future = client.writeAndFlush(buf);
//                ChannelFuture future = client.writeAndFlush(payload);
                boolean issuccess = future.isSuccess();
                log.info("下发isSuccess:"+future.isSuccess());
                log.info("下发isDone:"+future.isDone());
                log.info("下发异常:"+future.cause());
                log.info("下发取消:"+future.isCancelled());
                if(issuccess){
                    log.info("下发成功CMD503:"+payload);
                }else{
                    log.error("下发失败CMD503:"+future.cause());
                }
            }
            if(dto.getCmd().equals("505")){
                builder.append(StringUtils.leftPad(dto.getGunNo(), 2,"0"));
                builder.append(StringUtils.leftPad(dto.getModeID(), 16,"0"));
                for (ZDBillingDto.timeBilling time :dto.getTimeBillings()){
                    builder.append(StringUtils.leftPad(time.getStartHour(),2,"0"));
                    builder.append(StringUtils.leftPad(time.getStartMinute(),2,"0"));
                    builder.append(StringUtils.leftPad(time.getStopHour(),2,"0"));
                    builder.append(StringUtils.leftPad(time.getStopMinute(),2,"0"));
                    builder.append(StringUtils.leftPad(String.format("%.4f",time.getChargeFee()),8,"0"));
                    builder.append(StringUtils.leftPad(String.format("%.4f",time.getServiceFee()),8,"0"));
                }
                String payload =  ZDObjectUtil.pushMessage(builder.toString(),CommonUtil.toHexString(dto.getPileNo()), BizConstant.Cmd.CMD_505);
                client.writeAndFlush(payload);
            }
            if(dto.getCmd().equals("507")){
                builder.append(Integer.toHexString(Integer.valueOf(dto.getGunNo())));
                builder.append(Integer.toHexString(Integer.valueOf(dto.getModeID())));
                for (ZDBillingDto.powerBilling power :dto.getPowerBillings()){
                    builder.append(Integer.toHexString(Integer.valueOf(power.getStartPower())));
                    builder.append(Integer.toHexString(Integer.valueOf(power.getEndPower())));
                    builder.append(Integer.toHexString(Integer.valueOf(power.getFee())));
                }
                String payload =  ZDObjectUtil.pushMessage(builder.toString(), dto.getGunNo(), BizConstant.Cmd.CMD_507);
                ChannelFuture result = client.writeAndFlush(payload);
                boolean isdone = result.isDone();
                if(isdone){
                    log.info("下发成功：CMD"+dto.getCmd()+":"+payload);
                }else {
                    log.info("下发失败：CMD"+dto.getCmd()+":"+payload);
                }
            }
    }
}
